
//策略启动经理类
import weexHttp from "@/_js/https"; ;
const weexFunction = weex.requireModule("WeexFunction");

import modal from "@/_js/modal";
import broadcast from "@/_js/broadcast";
import API from "@/_js/api";
import mParam from "@/up/amParam";



var I18N = {};
I18N.en = {
  "启动成功":"Start successfully",
};
I18N.ja = {
  "启动成功":"起動成功",
};

I18N.ko = {
  "启动成功":"시작 성공",
};

var language = weex.config.language;
// var language = "en";
var msg = I18N[language];
if(!msg){
  msg = {};
}
var T = function(text){
    return msg[text] || text;
};


export default {
  
  /******************************************* 其他方法*/
  /** 启动成功
   * 
   * @param {String} apiId apiId 
   * @param {String} investCoin 投资币 
   * @param {String} investCoinnNum 投资币数量 
   */
  upOk(apiId, investCoin, investCoinnNum){
    modal.toast(T('启动成功'));
    broadcast.send('UpShow', {'index':1});
    apiId.length && API.addApiStrategy(apiId, investCoin, investCoinnNum);
  },

  /******************************************* http*/
  /** HTTP Ai启动
   * 
   * @param {String} address 服务器地址
   * 
   * @param {JSON} api api
   * @param {String} buyFee 手续费
   *  
   * @param {String} strategyId 策略Id
   * @param {String} exchangeId 交易所Id
   * @param {String} counterCoinId 交易币Id
   * @param {String} baseCoinId 本币Id
   * 
   * @param {String} investCoin 投资币
   * @param {String} investCoinnNum 投资数量
   *  
   * @param {Boolean} investType 投资方向：true交易币 false本币
   * @param {String} bannerId 类型ID: 保守、稳健、激进
   * 
   * @param {JSON} setMap 其他设置
   * @param {Function} success 成功回调
   */
  httpUpAi(param, success=()=>{}){

    var keys = ['address',
                'api', 'buyFee',
                'strategyId', 'exchangeId', 'counterCoinId' , 'baseCoinId',
                'investCoin', 'investCoinnNum', 
                'investType', 'bannerId', 
                'setMap'];
    if (mParam.checkParam(param, keys, true)) return;

    mParam.getApiParam(param.api, (apiMap)=>{

      param = Object.assign(param, apiMap);
      param = Object.assign(param, param.setMap);
      delete param['setMap'];
      param['startType'] = 'OFFICIAL';
      weexHttp.postX("strategyV3/aiStartExecute",param,true,(data)=>{

        modal.hideLoad();
        if(data.status == 200){
          this.upOk(apiMap.apiId, param.investCoin, param.investCoinnNum);
          success();
        } else {
          modal.toast(data.msg);
        }
      });
    });
  },

  /** HTTP 自定义启动
   * 
   * @param {String} address 服务器地址
   * 
   * @param {JSON} api api
   * @param {String} buyFee 手续费
   *  
   * @param {String} strategyId 策略Id
   * 
   * @param {String} investCoin 投资币
   * @param {String} investCoinnNum 投资数量
   *  
   * @param {Boolean} investType 投资方向：true交易币 false本币
   * @param {String} startType //策略类型：OFFICIAL官方策略 Buy购买策略 PRIVATE私有策略
   * 
   * @param {String} executeConfig //执行配置
   * @param {String} pairMap //交易对
   * 
   * @param {JSON} reserves 参数对象
   * @param {JSON} setMap 其他设置
   * 
   * @param {Boolean} isOpenFollow ture开通跟单（可选）
   * @param {JSON} followConfig 跟单配置（可选）
   * 
   * @param {Function} success 成功回调
   */
  httpUpDefine(param, success=()=>{}) {

    var keys = ['address',
                'api', 'buyFee',
                'strategyId', 
                'investCoin', 'investCoinnNum', 
                'investType', 'startType',
                'executeConfig', 'pairMap',
                'reserves', 'setMap'];
    if (mParam.checkParam(param, keys, true)) return;
   
    mParam.getApiParam(param.api, (apiMap)=>{

     var executeConfig = mParam.getExecuteConfig(param, apiMap);
     var isMimic = apiMap.apiId.length ? false : true;
     var paramMap = {'address':param.address,
                     'strategyId':param.strategyId, 
                     'startType':param.startType,
                     'investType':param.investType,
                     'isMimic':isMimic,
                     'executeConfig':JSON.stringify(executeConfig)};
     if (param.followConfig != null) paramMap['followConfig'] = param.followConfig;
     if (param.isOpenFollow != null) paramMap['isOpenFollow'] = param.isOpenFollow;

     weexHttp.postX("strategyV3/defineStartExecute", paramMap,true,(data)=>{

      modal.hideLoad();
       if(data.status == 200){
        this.upOk(apiMap.apiId, param.investCoin, param.investCoinnNum);
        success();
       } else {
        modal.toast(data.msg);
       }
     });
   });
  },

  /** HTTP 多币对启动
   * 
   * @param {String} address 服务器地址
   * 
   * @param {JSON} api api
   * @param {String} buyFee 手续费
   *  
   * @param {String} strategyId 策略Id
   * 
   * @param {String} investCoin 投资币
   * @param {String} investCoinnNum 投资数量
   *  
   * @param {Boolean} investType 投资方向：true交易币 false本币
   * 
   * @param {String} executeConfig //执行配置
   * @param {String} pairMap //交易对
   * @param {Array} pairs 多币对数组
   * 
   * @param {JSON} reserves 参数对象
   * @param {JSON} setMap 其他设置
   * 
   * @param {Boolean} isOpenFollow ture开通跟单（可选）
   * @param {JSON} followConfig 跟单配置（可选）
   * 
   * @param {Function} success 成功回调
   */
  httpUpMany(param, success=()=>{}, failure=()=>{}) {

    var keys = ['address',
                'api', 'buyFee',
                'strategyId', 
                'investCoin', 'investCoinnNum', 
                'investType',
                'executeConfig', 'pairMap', 'pairs',
                'reserves', 'setMap'];
    if (mParam.checkParam(param, keys, true)) return;

    mParam.getApiParam(param.api, (apiMap)=>{
      param['startType'] = 'OFFICIAL';
      var executeConfig = mParam.getExecuteConfig(param, apiMap);
      var isMimic = apiMap.apiId.length ? false : true;
      var paramMap = {'address':param.address,
                      'strategyId':param.strategyId, 
                      'startType':param.startType,
                      'investType':param.investType,
                      'isMimic':isMimic,
                      'executeConfig':JSON.stringify(executeConfig)};
      if (param.followConfig != null) paramMap['followConfig'] = param.followConfig;
      if (param.isOpenFollow != null) paramMap['isOpenFollow'] = param.isOpenFollow;

      weexHttp.postX("strategyV3/manyStartExecute",paramMap,true,(data)=>{
        modal.hideLoad();
        if(data.status == 200){
          this.upOk(apiMap.apiId, param.investCoin, param.investCoinnNum);
          success();
        } else {
          modal.toast(data.msg);
        }
      });
    });
  },
}
  